#include "ukuitaskbarpreviewwidget.h"
#include "ukuiwindow.h"

#include <QDebug>

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QIcon>
#include "ukuitaskbar.h"
#include <QMenu>
#include <QContextMenuEvent>

#include "ukuitaskclosebutton.h"


static int temp = 0;

UKUITaskbarPreviewWidget::UKUITaskbarPreviewWidget(UKUIWindow *window, UKUITaskBar *taskbar, QWidget *parent) :
    m_window(window),
    m_taskbar(taskbar),
    m_mainLayout(new QVBoxLayout(this))
{
    Q_ASSERT(window);

    initUI();
}

UKUITaskbarPreviewWidget::~UKUITaskbarPreviewWidget()
{

}


void UKUITaskbarPreviewWidget::setPreviewVisible(bool visible)
{
    mThumbnailLabel->hide();
}

void UKUITaskbarPreviewWidget::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu * menu = new QMenu(tr("Widget"),this);
    menu->setAttribute(Qt::WA_DeleteOnClose);
    /* 对应预览图右键功能 关闭 还原 最大化  最小化 置顶 取消置顶*/
    QAction *close = menu->addAction(QIcon::fromTheme("window-close-symbolic"), tr("close"));
    QAction *restore = menu->addAction(QIcon::fromTheme("window-restore-symbolic"), tr("restore"));

    QAction *maxim = menu->addAction(QIcon::fromTheme("window-maximize-symbolic"), tr("maximaze"));

    QAction *minim = menu->addAction(QIcon::fromTheme("window-minimize-symbolic"), tr("minimize"));
    QAction *above = menu->addAction(QIcon::fromTheme("ukui-fixed"), tr("above"));
    QAction *clear = menu->addAction(QIcon::fromTheme("ukui-unfixed"), tr("clear"));

    connect(close, SIGNAL(triggered()), this, SLOT(closeApplication()));
    connect(maxim, SIGNAL(triggered()), this, SLOT(maximizeApplication()));
    connect(restore, SIGNAL(triggered()), this, SLOT(deMaximizeApplication()));
    connect(minim, SIGNAL(triggered()), this, SLOT(minimizeApplication()));
    connect(above, SIGNAL(triggered()), this, SLOT(setWindowKeepAbove()));
    connect(clear, SIGNAL(triggered()), this, SLOT(setWindowStatusClear()));

    menu->exec(/*event->pos()*/);
    //menu->show();

}

void UKUITaskbarPreviewWidget::mousePressEvent(QMouseEvent *event)
{
//    const Qt::MouseButton b = event->button();

//    if (Qt::LeftButton == b)
//    {
//        mDragStartPosition = event->pos();
//    }
//    else if (Qt::MidButton == b && parentTaskBar()->closeOnMiddleClick())
//        closeApplication();

    if (Qt::LeftButton == event->button())
    {
        raiseApplication();
    }

    QWidget::mousePressEvent(event);
}

void UKUITaskbarPreviewWidget::updateIcon()
{
    QIcon ico;
    if (m_taskbar->isIconByClass())
    {
        //todo
        //ico = QIcon::fromTheme(QString::fromUtf8(KWindowInfo{mWindow, 0, NET::WM2WindowClass}.windowClassClass()).toLower());
    }
    if (ico.isNull())
    {
        ico = m_window->getIcon();
    }
    qDebug()<<"++++++++++++++++++++++++++++++++++++++++++";
    qDebug()<<m_window->getAppName()<<m_window->getTitle()<<ico;
    m_appIconLabel->setPixmap(ico.pixmap(QSize(19,19)));
}

void UKUITaskbarPreviewWidget::slotUpdateTitle()
{
    m_titleLabel->setText(m_window->getTitle());
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::white);
    m_titleLabel->setPalette(pa);
}

void UKUITaskbarPreviewWidget::closeApplication()
{
    Q_ASSERT(m_window);
    m_window->requestClose();
}

void UKUITaskbarPreviewWidget::maximizeApplication()
{
    Q_ASSERT(m_window);
    m_window->requestMax();

}

void UKUITaskbarPreviewWidget::minimizeApplication()
{
    Q_ASSERT(m_window);
    m_window->requestMin();
}

void UKUITaskbarPreviewWidget::deMaximizeApplication()
{
    m_window->requestActivate();
}

void UKUITaskbarPreviewWidget::raiseApplication()
{
    Q_ASSERT(m_window);
    qDebug()<<"m_window->requestRise()";
    m_window->requestActivate();
}

void UKUITaskbarPreviewWidget::setWindowKeepAbove()
{
    m_window->requestKeepAbove();
}

void UKUITaskbarPreviewWidget::setWindowStatusClear()
{
    m_window->requestToggleKeepBelow();
}

void UKUITaskbarPreviewWidget::initUI()
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    setMinimumWidth(1);
//    setMinimumHeight(1);
    //temp
    setMinimumHeight(46);

    setAcceptDrops(true);

    //setAttribute(Qt::WA_TranslucentBackground,false);//设置窗口背景透明
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowCloseButtonHint);   //设置无边框窗口    \

    QHBoxLayout *topbarLayout = new QHBoxLayout();
    topbarLayout->setContentsMargins(0,0,0,0);


    m_appIconLabel = new QLabel(this);
    m_appIconLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    updateIcon();

    m_titleLabel = new QLabel(this);
    m_titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    m_titleLabel->setMinimumWidth(1);
    m_titleLabel->setContentsMargins(0, 0, 5, 0);
    m_titleLabel->setMargin(0);

    connect(m_window,SIGNAL(titleChanged()), this, SLOT(slotUpdateTitle()));
    slotUpdateTitle();



    mCloseBtn =  new UKUITaskCloseButton(this);
//    mCloseBtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
    mCloseBtn->setIconSize(QSize(19,19));
    mCloseBtn->setFixedSize(QSize(19,19));
    connect(mCloseBtn, SIGNAL(sigClicked()), this, SLOT(closeApplication()));



    topbarLayout->addWidget(m_appIconLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);
    topbarLayout->addWidget(m_titleLabel, 10, Qt::AlignLeft);
    topbarLayout->addWidget(mCloseBtn, 0, Qt::AlignRight);


    m_mainLayout->addStretch();
    m_mainLayout->addLayout(topbarLayout);

    mThumbnailLabel = new QLabel("this",this);
    mThumbnailLabel->setScaledContents(true);
    mThumbnailLabel->setMinimumSize(QSize(1, 1));

    m_mainLayout->addWidget(mThumbnailLabel, Qt::AlignCenter, Qt::AlignCenter);
    m_mainLayout->addStretch();
    m_mainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
    this->setLayout(m_mainLayout);

    int winWidth = 246;

    int iPreviewPosition =  m_taskbar->plugin()->panel()->panelSize()/2 - winWidth/2;

    temp +=1;

    setAutoFillBackground(true);
//    if(temp==1)
//    {
    //setGeometry(m_taskbar->plugin()->panel()->calculatePopupWindowPos(mapToGlobal(QPoint(iPreviewPosition,0)), QSize(400,400)));
     //       this->show();
//    }
}
